﻿using Tisbi.LawFirm.DAL;

namespace Tisbi.LawFirm.BL
{
    public static class DBContextManager
    {
        public static LawContext DataBaseContext;

        static DBContextManager()
        {
            var connectionStringSettings = ConfigurationHelper.GetConnectionString();
            if (connectionStringSettings == null)
            {
                return;
            }

            DataBaseContext = new LawContext(connectionStringSettings.ConnectionString);
        }

        public static bool HasConnected()
        {
            try
            {
                var dbConnection = DataBaseContext.Database.Connection;
                dbConnection.Open();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static void Update()
        {
            var connectionStringSettings = ConfigurationHelper.GetConnectionString();
            if (connectionStringSettings == null)
            {
                return;
            }

            DataBaseContext = new LawContext(connectionStringSettings.ConnectionString);
        }
    }
}